<?php

namespace app\admin\controller;

use app\admin\model\Book as BookModel;
use app\BaseController;

class Book extends BaseController
{
    /* 列表 */
    public function index()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $cid = input('cid', 0, 'intval');
        $page = input('page', 1, 'intval');
        $perpage = 10;
        $result = BookModel::with('title')->order('time desc')->where('cid', $cid)->page($page, $perpage)->select();
        $total = BookModel::where('cid', $cid)->count();
        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $perpage,
            'data' =>  $result
        ];
        return show(config('status.success'), '获取列表', $data);
    }

    /* 删除 */
    public function delete()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', 0, 'intval');
        $result = BookModel::destroy($id);
        if ($result) {
            return show(config('status.success'), '删除成功');
        } else {
            return show(config('status.error_delete'), '删除失败');
        }
    }

    /* 批量删除 */
    public function deleteAll()
    {
        if (!$this->request->auth) return show(config('status.auth'), 'Authorization Failed');
        $id = input('id', '');
        $arr = explode(',', $id);
        $result = BookModel::destroy($arr);
        if ($result) {
            return show(config('status.success'), '删除成功');
        } else {
            return show(config('status.error_delete'), '删除失败');
        }
    }
}
